﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.IO;

namespace Lamber.JCP.JCPReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class JCPReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// An item is being updated
        /// </summary>
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            this.EventFiringEnabled = false;
            try
            {
                SPListItem item = properties.ListItem;
                bool toUpdate = false;
                bool activated = false;
                if (properties.AfterProperties["jcp_activated"] != null)
                {
                    Boolean.TryParse(properties.AfterProperties["jcp_activated"].ToString(), out activated);
                }

                Guid activationId = Guid.Empty;
                if (properties.AfterProperties["jcp_actionId"] != null && !String.IsNullOrEmpty(properties.AfterProperties["jcp_actionId"].ToString()))
                {
                    activationId = new Guid(properties.AfterProperties["jcp_actionId"].ToString());
                }

                if ((activated) && (activationId == Guid.Empty))
                {
                    String extention = Path.GetExtension(item.Url);

                    SPUserCustomAction action = properties.Web.Site.UserCustomActions.Add();
                    action.ScriptSrc = String.Format("~siteCollection/{0}", item.Url);
                    action.Sequence = int.Parse(properties.AfterProperties["jcp_sequence"].ToString());

                    action.Location = "ScriptLink";
                    action.Update();
                    item["jcp_activated"] = true;
                    item["jcp_actionId"] = action.Id;

                    toUpdate = true;
                }

                if ((activated) && (!item["jcp_sequence"].ToString().Equals(properties.AfterProperties["jcp_sequence"].ToString(), StringComparison.CurrentCultureIgnoreCase)))
                {
                    SPUserCustomAction action = properties.Web.Site.UserCustomActions[activationId];
                    action.Sequence = int.Parse(properties.AfterProperties["jcp_sequence"].ToString());
                    action.Update();
                }

                if ((!activated) && (activationId != Guid.Empty))
                {
                    SPUserCustomAction action = properties.Web.Site.UserCustomActions[activationId];
                    action.Delete();
                    item["jcp_activated"] = false;
                    item["jcp_actionId"] = null;
                    toUpdate = true;
                }

                if (toUpdate)
                {
                    item.Update();
                }
            }
            finally
            {
                this.EventFiringEnabled = true;
                base.ItemUpdating(properties);
            }
        }
    }
}
